﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;

namespace Light.Utility
{

    /// <summary>
    /// XML取值工具类
    /// </summary>
    public static class XmlHelper
    {


        #region GetAttrbuteValue

        public static string GetAttributeValue(XElement element, string attributeName)
        {
            return GetAttributeValue(element, attributeName, string.Empty);
        }

        public static string GetAttributeValue(XElement element, string attributeName, string defaultValue)
        {
            XAttribute attr = element.Attribute(attributeName);

            if (attr != null && !String.IsNullOrEmpty(attr.Value))
                defaultValue = attr.Value;

            return defaultValue;
        }

        public static bool GetAttributeValue(XElement element, string attributeName, bool defaultValue)
        {

            bool value = false;

            XAttribute attr = element.Attribute(attributeName);
            if (attr != null && bool.TryParse(attr.Value, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }

        public static int GetAttributeValue(XElement element, string attributeName, int defaultValue)
        {

            int value = 0;

            XAttribute attr = element.Attribute(attributeName);
            if (attr != null && int.TryParse(attr.Value, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }

        public static decimal GetAttributeValue(XElement element, string attributeName, decimal defaultValue)
        {
            decimal value = 0;

            XAttribute attr = element.Attribute(attributeName);
            if (attr != null && decimal.TryParse(attr.Value, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }

        public static float GetAttributeValue(XElement element, string attributeName, float defaultValue)
        {
            float value = 0;

            XAttribute attr = element.Attribute(attributeName);
            if (attr != null && float.TryParse(attr.Value, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }

        public static string GetAttributeValue(XmlElement element, string attributeName)
        {
            return GetAttributeValue(element, attributeName, string.Empty);
        }

        public static string GetAttributeValue(XmlElement element, string attributeName, string defaultValue)
        {
            string value = defaultValue;

            defaultValue = element.GetAttribute(attributeName);

            if (!String.IsNullOrEmpty(defaultValue))
                value = defaultValue;

            return value;
        }

        public static bool GetAttributeValue(XmlElement element, string attributeName, bool defaultValue)
        {

            bool result = defaultValue;
            int result1 = 0;
            string value = element.GetAttribute(attributeName);
            if (bool.TryParse(value, out defaultValue))
            {
                result = defaultValue;
            }
            else if (int.TryParse(value, out result1))
            {
                result = result1 > 0;
            }

            return result;
        }

        public static int GetAttributeValue(XmlElement element, string attributeName, int defaultValue)
        {

            int result = defaultValue;

            string value = element.GetAttribute(attributeName);
            if (int.TryParse(value, out defaultValue))
            {
                result = defaultValue;
            }

            return result;
        }

        public static decimal GetAttributeValue(XmlElement element, string attributeName, decimal defaultValue)
        {

            decimal result = defaultValue;

            string value = element.GetAttribute(attributeName);
            if (decimal.TryParse(value, out defaultValue))
            {
                result = defaultValue;
            }

            return result;
        }

        public static float GetAttributeValue(XmlElement element, string attributeName, float defaultValue)
        {

            float result = defaultValue;

            string value = element.GetAttribute(attributeName);
            if (float.TryParse(value, out defaultValue))
            {
                result = defaultValue;
            }

            return result;
        }


        #endregion


        #region GetElementValue


        public static string GetValue(XElement element)
        {
            return GetValue(element, string.Empty);
        }

        public static string GetValue(XElement element, string defaultValue)
        {
            string value = element.Value;

            if (value != null)
                defaultValue = value;

            return defaultValue;
        }

        public static bool GetValue(XElement element, bool defaultValue)
        {

            bool value = false;

            string eValue = element.Value;
            if (eValue != null && bool.TryParse(eValue, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }

        public static int GetValue(XElement element, int defaultValue)
        {

            int value = 0;

            string eValue = element.Value;
            if (eValue != null && int.TryParse(eValue, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }
        public static decimal GetValue(XElement element, decimal defaultValue)
        {

            decimal value = 0;

            string eValue = element.Value;
            if (eValue != null && decimal.TryParse(eValue, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }

        public static float GetValue(XElement element, float defaultValue)
        {

            float value = 0;

            string eValue = element.Value;
            if (eValue != null && float.TryParse(eValue, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }


        public static string GetValue(XmlElement element)
        {
            return GetValue(element, string.Empty);
        }

        public static string GetValue(XmlElement element, string defaultValue)
        {
            string value = element.Value;

            if (value != null)
                defaultValue = value;

            return defaultValue;
        }

        public static bool GetValue(XmlElement element, bool defaultValue)
        {

            bool value = false;
            int result1 = 0;

            string eValue = element.Value;
            if (eValue != null && bool.TryParse(eValue, out value))
            {
                defaultValue = value;
            }
            else if (int.TryParse(eValue, out result1))
            {
                defaultValue = result1 > 0;
            }
            return defaultValue;
        }

        public static int GetValue(XmlElement element, int defaultValue)
        {

            int value = 0;

            string eValue = element.Value;
            if (eValue != null && int.TryParse(eValue, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }
        public static decimal GetValue(XmlElement element, decimal defaultValue)
        {

            decimal value = 0;

            string eValue = element.Value;
            if (eValue != null && decimal.TryParse(eValue, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }

        public static float GetValue(XmlElement element, float defaultValue)
        {

            float value = 0;

            string eValue = element.Value;
            if (eValue != null && float.TryParse(eValue, out value))
            {
                defaultValue = value;
            }

            return defaultValue;
        }



        #endregion
    }
}
